from django_filters import *
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework.filters import SearchFilter, OrderingFilter

from mysqldb.models import *


class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义list过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)


class GameLolHonebaoListFilterSet(FilterSet):
    """游戏成就过滤器"""

    pid = NumberFilter(field_name="pid", required=False, help_text="场所ID")
    mid = NumberFilter(field_name="mid", required=False, help_text="会员ID")
    status = NumberFilter(field_name="status", required=False, help_text="状态")
    answer = NumberFilter(field_name="answer", required=False, help_text="操作员")
    computername = CharFilter(field_name="computername", help_text="终端号")
    stardate = DateFilter(
        field_name="currentime",
        required=False,
        method="filter_stardate",
        help_text="开始日期",
    )
    enddate = DateFilter(
        field_name="currentime",
        required=False,
        method="filter_enddate",
        help_text="结束日期",
    )

    def filter_stardate(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_enddate(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})

    class Meta:
        model = GameLolHonebaoList
        fields = [
            "pid",
            "mid",
            "status",
            "answer",
            "computername",
            "stardate",
            "enddate",
        ]


class GameLolChessListFilterSet(FilterSet):
    """对局过滤器"""

    pid = NumberFilter(field_name="pid", required=False, help_text="场所ID")
    computer = CharFilter(field_name="computer", help_text="终端号")
    puuid = CharFilter(field_name="puuid", help_text="游戏ID")
    stardate = DateTimeFilter(
        field_name="gamestarttime",
        required=False,
        method="filter_stardate",
        help_text="开始时间",
    )
    enddate = DateTimeFilter(
        field_name="gameendtime",
        required=False,
        method="filter_enddate",
        help_text="结束时间",
    )

    def filter_stardate(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_enddate(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})

    class Meta:
        model = GameLolChessList
        fields = [
            "pid",
            "computer",
            "puuid",
            "stardate",
            "enddate",
        ]


class GameLolRewardListFilterSet(FilterSet):
    """对局过滤器"""

    pid = NumberFilter(field_name="pid", required=False, help_text="场所ID")
    computer = CharFilter(field_name="computer", help_text="终端号")
    puuid = CharFilter(field_name="puuid", help_text="游戏ID")
    stardate = DateTimeFilter(
        field_name="gamestarttime",
        required=False,
        method="filter_stardate",
        help_text="开始时间",
    )
    enddate = DateTimeFilter(
        field_name="gameendtime",
        required=False,
        method="filter_enddate",
        help_text="结束时间",
    )

    def filter_stardate(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_enddate(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})

    class Meta:
        model = GameLolRewardList
        fields = [
            "pid",
            "computer",
            "puuid",
            "stardate",
            "enddate",
        ]
